<?php

class Page_Category extends Model 
{
	protected $_table_name = TB_PAGE_CATEGORY;
	protected $_primary_key = 'id';	
	
	function __construct()
	{
		parent::__construct();
	}
	
	public function loadMenuTree($page_id, $default_language, $parent_id = 0)
	{
		$arrMenuTree = array();
		
		$sql = "SELECT pr.id ,pr.uniqid ,pr.page_id ,pr.code ,pr.parent_id ,pr.active ,pr.sort_order ,pr.last_update ,ci.name FROM ".TB_PAGE_CATEGORY. " as pr INNER JOIN ".TB_PAGE_CATEGORY_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.page_id = ? AND pr.parent_id = ? AND ci.ln = ? AND pr.active = 1";
		$sql .= " ORDER BY `sort_order` ASC";		
		
		$sth = $this->query($sql, array($page_id ,$parent_id ,$default_language));
		
		$rsSubMenu = $sth->fetchAll(PDO::FETCH_ASSOC);
		
		foreach ($rsSubMenu as $row)
		{
			$row['sub_menus'] = $this->loadMenuTree($page_id, $default_language, $row['id']);			
			$arrMenuTree[] = $row;
		}
		
		return $arrMenuTree;
	}	
	
	public function loadMenuTreeOptionHtml($page_id ,$control_name ,$selected_id ,$reject_sub_menu_id = -1 , $cfg_languages)
	{
		$str_html = "<select tabindex='1' name='{$control_name}' id='{$control_name}' data-placeholder='Choose a Category' class='span7'>";
		
		if(is_null($selected_id))
			$str_html .= "<option value=''>Please select...</option>";
		$sel = $selected_id == 0 ? "selected='selected'" : "";
		$str_html .= "<option value='0' {$sel}>ROOT</option>";			
		
		$sql = "SELECT pr.id ,pr.uniqid ,pr.page_id ,pr.parent_id ,pr.active ,pr.sort_order ,pr.last_update ,ci.name FROM ".TB_PAGE_CATEGORY. " as pr INNER JOIN ".TB_PAGE_CATEGORY_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.page_id = ? AND pr.parent_id = 0 AND ci.ln = ? AND pr.active = 1";
		$sql .= " ORDER BY `sort_order` ASC";
		
		$sth = $this->query($sql, array($page_id ,$cfg_languages['default_lang']));
		$rsSubMenu = $sth->fetchAll(PDO::FETCH_ASSOC);				
		
		$counter = 1;
		$padding = 20*$counter;
		
		foreach ($rsSubMenu as $row)
		{
			if ($row["id"] != $reject_sub_menu_id)
			{
				$sel = null;
				if ($row["id"] == $selected_id)
					$sel = "selected='selected'";
								
				$str_html .= "<option style='padding-left: {$padding}px' value='".$row["id"]."' {$sel}>|--&nbsp;".h($row["name"])."</option>";
				$str_html .= $this->recur_loadMenuTreeOptionHtml($selected_id ,$reject_sub_menu_id ,$row ,$counter + 1 ,$cfg_languages);
			}
		}
		
		$str_html .= "</select>";
		return $str_html;
	}
	
	private function recur_loadMenuTreeOptionHtml($selected_id ,$reject_sub_menu_id = -1 ,$rowMenu = NULL, $counter, $cfg_languages)
	{
		$padding = 20*$counter;
		$str_html = "";
		
		$sql = "SELECT pr.id ,pr.uniqid ,pr.page_id ,pr.parent_id ,pr.active ,pr.sort_order ,pr.last_update ,ci.name FROM ".TB_PAGE_CATEGORY. " as pr INNER JOIN ".TB_PAGE_CATEGORY_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.parent_id = ? AND ci.ln = ? AND pr.active = 1";
		$sql .= " ORDER BY `sort_order` ASC";
		
		$sth = $this->query($sql, array($rowMenu['id'] ,$cfg_languages['default_lang']));
		$rsSubMenu = $sth->fetchAll(PDO::FETCH_ASSOC);		
		
		foreach ($rsSubMenu as $row)
		{
			if ($row["id"] != $reject_sub_menu_id)
			{
				$sel = NULL;
				if ($row["id"] == $selected_id)
					$sel = "selected='selected'";
				
				$str_html .= "<option style='padding-left: {$padding}px' value='".$row["id"]."' {$sel}>|--&nbsp;".h($row["name"])."</option>";
				$str_html .= $this->recur_loadMenuTreeOptionHtml($selected_id ,$reject_sub_menu_id ,$row ,$counter + 1 ,$cfg_languages);
			}			
		}
		return $str_html;		
	}
	
	public function loadRowSetCat($parent_id, $ln_code)
	{
		$sql = "SELECT pr.id ,pr.code ,pr.uniqid ,pr.active ,pr.sort_order ,pr.last_update ,pr.create_at ,ci.name FROM ".TB_PAGE_CATEGORY. " as pr INNER JOIN ".TB_PAGE_CATEGORY_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.parent_id = ? AND ci.ln = ? ";
		$sql .= " ORDER BY `sort_order` ASC";
		
		$sth = $this->query($sql, array($parent_id, $ln_code));
		
		return $sth->fetchAll(PDO::FETCH_ASSOC);		
	}
	
	function getRowDataCategory($cat_id)
	{
		$rowCat = $this->get($cat_id);
	
		$sql = "SELECT ci.* FROM ".TB_PAGE_CATEGORY_LN." as ci WHERE ci.id = ?";
	
		$sth = $this->query($sql, array($cat_id));
		$arr =	$sth->fetchAll(PDO::FETCH_ASSOC);
		
		$rowLnCat = array();
		foreach ($arr as $row)
		{
			$rowLnCat["{$row['ln']}"] = $row;
		}
	
		$data = array();
		$data['main_cat_field'] = $rowCat;
		$data['ln_cat_field'] = $rowLnCat;
		
		return $data;
	}	
	
	function insertCategory($page_id, $arrMainField ,$arrLnField)
	{
		$arrMainField['page_id'] = $page_id;
		$arrMainField['uniqid'] = create_uniqid(10);
		$arrMainField['create_at'] = now_to_mysql();
		// TODO : sort_order
		$id = $this->insert($arrMainField);
	
		// Update sort_order
		$data['sort_order'] = $id;
		$this->update($id, $data);
	
		$this->_table_name = TB_PAGE_CATEGORY_LN;
		foreach ($arrLnField as $ln => $arrData)
		{
			$arrData['id'] = $id;
			$arrData['ln'] = $ln;
			$arrData['create_at'] = now_to_mysql();
			$this->insert($arrData);
		}
		$this->_table_name = TB_PAGE_CATEGORY;
		
		return $id;
	}
	
	function updateCategory($cat_id, $arrMainField ,$arrLnField)
	{
		// TODO : sort_order
		$this->update($cat_id, $arrMainField);
	
		$this->_table_name = TB_PAGE_CATEGORY_LN;
		foreach ($arrLnField as $ln => $arrData)
		{
			$this->updateWithCondition("id = {$cat_id} AND ln = '{$ln}'",$arrData);
		}
		$this->_table_name = TB_PAGE_CATEGORY;
	}
	
	function deleteCategory($cat_id)
	{
		$sql = "DELETE * FROM ".TB_PAGE_CATEGORY_LN;
		$sql .= " WHERE id = ?";
	
		$sth = $this->query($sql, array($cat_id));
	
		return $this->delete($cat_id);
	}	
	
	function deleteImage($action, $cat_id, $ln_code)
	{
		switch ($action) {
			case 'main-image':
				$row = $this->get($cat_id);
				@unlink(__UPLOAD_DATA_PATH.$row['image']);
				@unlink(__UPLOAD_DATA_PATH.'thumb_'.$row['image']);
				$this->update($cat_id,array('image'=>''));
				break;				
			
			case 'main-icon':
				$row = $this->get($cat_id);
				@unlink(__UPLOAD_DATA_PATH.$row['icon']);
				$this->update($cat_id,array('icon'=>''));
				break;
				
			case 'ln-image':
				$sql = "SELECT ci.* FROM ".TB_PAGE_CATEGORY_LN." as ci WHERE ci.id = ? AND ci.ln = ?";
				$sth = $this->query($sql, array($cat_id, $ln_code));
				$arr =	$sth->fetchAll(PDO::FETCH_ASSOC);
				
				if(empty($arr[0]))
					break;
				$row = $arr[0];
				
				@unlink(__UPLOAD_DATA_PATH.$row['ln_image']);
				@unlink(__UPLOAD_DATA_PATH.'thumb_'.$row['ln_image']);
				
				$this->_table_name = TB_PAGE_CATEGORY_LN;
				$this->updateWithCondition("id = '{$cat_id}' AND ln = '$ln_code'",array('ln_image'=>''));
				$this->_table_name = TB_PAGE_CATEGORY;
				break;
					
			case 'ln-icon':
				$sql = "SELECT ci.* FROM ".TB_PAGE_CATEGORY_LN." as ci WHERE ci.id = ? AND ci.ln = ?";
				$sth = $this->query($sql, array($cat_id, $ln_code));
				$arr =	$sth->fetchAll(PDO::FETCH_ASSOC);
				
				if(empty($arr[0]))
					break;
				$row = $arr[0];
				
				@unlink(__UPLOAD_DATA_PATH.$row['ln_icon']);
				
				$this->_table_name = TB_PAGE_CATEGORY_LN;
				$this->updateWithCondition("id = '{$cat_id}' AND ln = '$ln_code'",array('ln_icon'=>''));
				$this->_table_name = TB_PAGE_CATEGORY;
				break;				
			
			default:
				;
			break;
		}
		
	}
}

?>